package com.thb.pay.db.utils;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.google.common.collect.Lists;
import com.thb.pay.core.utils.PageResult;
import org.springframework.beans.BeanUtils;

import java.util.List;

/**
 * @Description 返回数据封装
 * @Author lucky
 * @data 2019/7/22 23:31
 */
public class PageResultUtils {

    public static <T> PageResult<T> build(IPage resultDBPage, Class<T> targetClazz) {
        if (resultDBPage == null) {
            return null;
        }
        List<T> records = Lists.newArrayList();
        resultDBPage.getRecords().forEach(entity -> {
            T target = null;
            try {
                target = targetClazz.newInstance();
            } catch (Exception e) {
            }
            BeanUtils.copyProperties(entity, target);
            records.add(target);
        });
        PageResult<T> result = new PageResult<>(resultDBPage.getCurrent(), resultDBPage.getSize(), resultDBPage.getTotal(), records);
        return result;
    }
}
